var index = require('../controller/index.js')
var user = require('../controller/user.js')
var post = require('../controller/post.js')
var comment = require('../controller/comment.js')
//引入json文件的方法
var carArr = require('../config/car.json').carList
//导出整个路由模块
module.exports = function(app){

	app.use(function(req,res,next){
    //在全局中调用，若在session中存在USER对象，就将USER存在本地对象中
	var _user = req.session.USER
	app.locals.USER = _user
    //调用next方法，执行到下一步骤
	next()

	})

	//首页路由

	app.get('/index',index.index)

	//登录页路由

	app.get('/signin',user.checkNoLogin,user.signin)
	app.post('/signin',user.forwardSignin)
	app.post('/signin/access',user.verifySigin)

	//注册页路由
	app.get('/signup',user.checkNoLogin,user.signup)
	app.post('/signup/save',user.saveUser)
	app.post('/signup/verifyEmail',user.verifyEmail)

	//登出页路由
	
	app.get('/logout',user.checkLogin,user.logout)


	//发布话题页

	app.get('/post',user.checkLogin,post.post)
	app.post('/post',post.savePost)


	//详细页

	app.get('/topic/:id',post.findPost)

	//评论相关

	app.post('/saveComment',comment.saveComment)
	app.post('/updateComment',comment.updateComment)

	//小马手机相关api
	app.get('/car',function(req,res){

		res.render('car',{
			'carArr':carArr.slice(0,5),
			'totalNum':carArr.length
		})
		
	})

  app.get('/carlogin',function(req,res){
    res.render('login',{})
  })

  app.get('/cardetail',function(req,res){
    res.render('car_detail',{})
  })

	app.post('/car/add',function(req,res){

		var start = parseInt(req.body.start)
		var step = parseInt(req.body.step)
		var end = start + step

		console.log(start)

		console.log(end)

		res.json({

			"totalNum":carArr.length,
			"carArr":carArr.slice(start,end)
		})
	})

}